package com.nchu.student_score.service.impl;

import com.nchu.student_score.dataObject.PasswordCheck;
import com.nchu.student_score.dataObject.User;
import com.nchu.student_score.mapper.AdminMapper;
import com.nchu.student_score.mapper.StudentMapper;
import com.nchu.student_score.mapper.TeacherMapper;
import com.nchu.student_score.model.Admin;
import com.nchu.student_score.model.Student;
import com.nchu.student_score.model.Teacher;
import com.nchu.student_score.service.UserService;
import com.nchu.student_score.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public ResultVo<User> adminCheck(User user) {
        Admin admin;
        ResultVo<User> resultVo = new ResultVo<>();

        try{
            admin = adminMapper.selectByPrimaryKey(Integer.valueOf(user.getId()));
        }
        catch (NumberFormatException e){
            admin = null;
        }

        if(admin != null && PasswordCheck.checkUserKey(user.getPassword(),admin.getPassword())){
            resultVo.setStatus(0);
            resultVo.setMessage("身份校验正常!");
        }
        else {
            resultVo.setStatus(1);
            resultVo.setMessage("身份校验不通过!");
        }

        return resultVo;
    }

    @Override
    public ResultVo<User> teacherCheck(User user) {
        Teacher teacher;
        ResultVo<User> resultVo = new ResultVo<>();

        try{
            teacher = teacherMapper.selectByPrimaryKey(Integer.valueOf(user.getId()));
        }
        catch (NumberFormatException e){
            teacher = null;
        }

        if(teacher != null && PasswordCheck.checkUserKey(user.getPassword(),teacher.getTeacherPassword())){
            resultVo.setStatus(0);
            resultVo.setMessage("身份校验正常!");
        }
        else {
            resultVo.setStatus(1);
            resultVo.setMessage("身份校验不通过!");
        }

        return resultVo;
    }

    @Override
    public ResultVo<User> studentCheck(User user) {
        Student student;
        ResultVo<User> resultVo = new ResultVo<>();

        try{
            student = studentMapper.selectByPrimaryKey(Integer.valueOf(user.getId()));
        }
        catch (NumberFormatException e){
            student = null;
        }

        if(student != null && PasswordCheck.checkUserKey(user.getPassword(),student.getStuPassword())){
            resultVo.setStatus(0);
            resultVo.setMessage("身份校验正常!");
        }
        else {
            resultVo.setStatus(1);
            resultVo.setMessage("身份校验不通过!");
        }

        return resultVo;
    }

    @Override
    public ResultVo<User> adminChangePwd(User user) {
        Admin admin;
        ResultVo<User> resultVo = new ResultVo<>();

        try{
            admin = adminMapper.selectByPrimaryKey(Integer.valueOf(user.getId()));
        }
        catch (NumberFormatException e){
            admin = null;
        }

        if(admin == null){
            resultVo.setStatus(1);
            resultVo.setMessage("管理员id不存在!");
            return resultVo;
        }

        String oldPwd = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());

        if(! admin.getPassword().equals(oldPwd)){
            resultVo.setStatus(2);
            resultVo.setMessage("您输入的原密码不正确!");
            return resultVo;
        }

        String newPwd = DigestUtils.md5DigestAsHex(user.getNewPwd().getBytes());
        admin.setPassword(newPwd);

        try {
            adminMapper.updateByPrimaryKey(admin);
        }
        catch (Exception e){
            e.printStackTrace();
            System.out.println();
            System.out.print("管理员" + admin.getAdminId() + "修改密码失败!");
            resultVo.setStatus(3);
            resultVo.setMessage("因其它原因修改密码不成功!");
            return resultVo;
        }

        resultVo.setStatus(0);
        resultVo.setMessage("修改密码成功!");
        return resultVo;
    }

    @Override
    public ResultVo<User> teacherChangePwd(User user) {
        Teacher teacher;
        ResultVo<User> resultVo = new ResultVo<>();

        try{
            teacher = teacherMapper.selectByPrimaryKey(Integer.valueOf(user.getId()));
        }
        catch (NumberFormatException e){
            teacher = null;
        }

        if(teacher == null){
            resultVo.setStatus(1);
            resultVo.setMessage("教师id不存在!");
            return resultVo;
        }

        String oldPwd = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());

        if(! teacher.getTeacherPassword().equals(oldPwd)){
            resultVo.setStatus(2);
            resultVo.setMessage("您输入的原密码不正确!");
            return resultVo;
        }

        String newPwd = DigestUtils.md5DigestAsHex(user.getNewPwd().getBytes());
        teacher.setTeacherPassword(newPwd);

        try {
            teacherMapper.updateByPrimaryKey(teacher);
        }
        catch (Exception e){
            e.printStackTrace();
            System.out.println();
            System.out.print("教师" + teacher.getTeacherId() + "修改密码失败!");
            resultVo.setStatus(3);
            resultVo.setMessage("因其它原因修改密码不成功!");
            return resultVo;
        }

        resultVo.setStatus(0);
        resultVo.setMessage("修改密码成功!");
        return resultVo;
    }

    @Override
    public ResultVo<User> studentChangePwd(User user) {
        Student student;
        ResultVo<User> resultVo = new ResultVo<>();

        try{
            student = studentMapper.selectByPrimaryKey(Integer.valueOf(user.getId()));
        }
        catch (NumberFormatException e){
            student = null;
        }

        if(student == null){
            resultVo.setStatus(1);
            resultVo.setMessage("学生id不存在!");
            return resultVo;
        }

        String oldPwd = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());

        if(! student.getStuPassword().equals(oldPwd)){
            resultVo.setStatus(2);
            resultVo.setMessage("您输入的原密码不正确!");
            return resultVo;
        }

        String newPwd = DigestUtils.md5DigestAsHex(user.getNewPwd().getBytes());
        student.setStuPassword(newPwd);

        try {
            studentMapper.updateByPrimaryKey(student);
        }
        catch (Exception e){
            e.printStackTrace();
            System.out.println();
            System.out.print("学生" + student.getStuId() + "修改密码失败!");
            resultVo.setStatus(3);
            resultVo.setMessage("因其它原因修改密码不成功!");
            return resultVo;
        }

        resultVo.setStatus(0);
        resultVo.setMessage("修改密码成功!");
        return resultVo;
    }
}
